/*
 * SaveSparqlTemplate.java
 *
 * Created on 9. Juni 2008, 00:37
 */

package twi.view;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import jtools.gui.CodeCompletionTextArea;

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;

import twi.KeySearch;
import twi.query.QueryTemplate;
import twi.sparql.grammer.SparqlList;
import twi.sparql.grammer.twi_sparqlLexer;
import twi.sparql.grammer.twi_sparqlParser;
import twi_project.TWI;

/**
 * 
 * @author Soeren
 */
public class SaveSparqlTemplate extends javax.swing.JDialog {

	/** Creates new form SaveSparqlTemplate */
	public SaveSparqlTemplate(java.awt.Frame parent, boolean modal, String query) {
		super(parent, modal);
		initComponents();
		((CodeCompletionTextArea) this.jTextArea_search).setWordList(KeySearch
				.getCompleteVocabulary(TWI.getInstance().getOntologieModel()));
		this.jTextArea_sparql.setText(query);
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated
	// Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jSplitPane1 = new javax.swing.JSplitPane();
		jPanel1 = new javax.swing.JPanel();
		jScrollPane3 = new javax.swing.JScrollPane();
		jTextArea_help = new javax.swing.JTextArea();
		jPanel2 = new javax.swing.JPanel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTextArea_sparql = new javax.swing.JTextArea();
		jLabel2 = new javax.swing.JLabel();
		list_var = new java.awt.List();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		list_domain = new java.awt.List();
		jScrollPane2 = new javax.swing.JScrollPane();
		jTextArea_search = new CodeCompletionTextArea();
		jButton_insert = new javax.swing.JButton();
		jPanel3 = new javax.swing.JPanel();
		jRadioButton_combo = new javax.swing.JRadioButton();
		jRadioButton_text = new javax.swing.JRadioButton();
		jRadioButton_tree = new javax.swing.JRadioButton();
		jButton_delete = new javax.swing.JButton();
		jButton_close = new javax.swing.JButton();
		jButton_save = new javax.swing.JButton();
		jLabel5 = new javax.swing.JLabel();
		jTextField_name = new javax.swing.JTextField();
		jLabel6 = new javax.swing.JLabel();
		jScrollPane4 = new javax.swing.JScrollPane();
		jTextArea1 = new javax.swing.JTextArea();
		list_lists = new java.awt.List();
		jLabel7 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
		jLabel1.setText("Save Template");
		jLabel1.setName("jLabel1"); // NOI18N

		jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
		jSplitPane1.setName("jSplitPane1"); // NOI18N
		jSplitPane1.setOneTouchExpandable(true);

		jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("help"));
		jPanel1.setName("jPanel1"); // NOI18N

		jScrollPane3.setName("jScrollPane3"); // NOI18N

		jTextArea_help.setColumns(20);
		jTextArea_help.setEditable(false);
		jTextArea_help.setRows(5);
		jTextArea_help.setEnabled(false);
		jTextArea_help.setName("jTextArea_help"); // NOI18N
		jScrollPane3.setViewportView(jTextArea_help);

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 360,
				Short.MAX_VALUE));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.PREFERRED_SIZE));

		jSplitPane1.setTopComponent(jPanel1);

		jPanel2.setName("jPanel2"); // NOI18N

		jScrollPane1.setName("jScrollPane1"); // NOI18N

		jTextArea_sparql.setColumns(20);
		jTextArea_sparql.setRows(5);
		jTextArea_sparql.setName("jTextArea_sparql"); // NOI18N
		jTextArea_sparql.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				jTextArea_sparqlKeyPressed(evt);
			}
		});
		jScrollPane1.setViewportView(jTextArea_sparql);

		jLabel2.setText("Sparql");
		jLabel2.setName("jLabel2"); // NOI18N

		list_var.setName("list_var"); // NOI18N
		list_var.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseEntered(java.awt.event.MouseEvent evt) {
				list_varMouseEntered(evt);
			}
		});
		list_var.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				list_varActionPerformed(evt);
			}
		});

		jLabel3.setText("Variables");
		jLabel3.setName("jLabel3"); // NOI18N

		jLabel4.setText("Range");
		jLabel4.setName("jLabel4"); // NOI18N

		list_domain.setName("list_domain"); // NOI18N
		list_domain.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseEntered(java.awt.event.MouseEvent evt) {
				list_domainMouseEntered(evt);
			}
		});
		list_domain.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				list_domainActionPerformed(evt);
			}
		});

		jScrollPane2.setName("jScrollPane2"); // NOI18N

		jTextArea_search.setColumns(20);
		jTextArea_search.setRows(1);
		jTextArea_search.setName("jTextArea_search"); // NOI18N
		jTextArea_search.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseEntered(java.awt.event.MouseEvent evt) {
				jTextArea_searchMouseEntered(evt);
			}
		});
		jTextArea_search.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				jTextArea_searchKeyPressed(evt);
			}
		});
		jScrollPane2.setViewportView(jTextArea_search);

		jButton_insert.setText("Insert");
		jButton_insert.setName("jButton_insert"); // NOI18N
		jButton_insert.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseEntered(java.awt.event.MouseEvent evt) {
				jButton_insertMouseEntered(evt);
			}
		});
		jButton_insert.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_insertActionPerformed(evt);
			}
		});

		jPanel3.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Autocreate GUI option"));
		jPanel3.setName("jPanel3"); // NOI18N

		jRadioButton_combo.setSelected(true);
		jRadioButton_combo.setText("Show As Combobox");
		jRadioButton_combo.setName("jRadioButton_combo"); // NOI18N
		jRadioButton_combo
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jRadioButton_comboActionPerformed(evt);
					}
				});

		jRadioButton_text.setText("Show as TextArea with Completion");
		jRadioButton_text.setName("jRadioButton_text"); // NOI18N
		jRadioButton_text
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jRadioButton_textActionPerformed(evt);
					}
				});

		jRadioButton_tree.setText("Use Tree Graphic");
		jRadioButton_tree.setEnabled(false);
		jRadioButton_tree.setName("jRadioButton_tree"); // NOI18N
		jRadioButton_tree
				.addActionListener(new java.awt.event.ActionListener() {
					public void actionPerformed(java.awt.event.ActionEvent evt) {
						jRadioButton_treeActionPerformed(evt);
					}
				});

		javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(
				jPanel3);
		jPanel3.setLayout(jPanel3Layout);
		jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel3Layout.createSequentialGroup().addGroup(
						jPanel3Layout.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(jRadioButton_combo).addComponent(
										jRadioButton_text).addComponent(
										jRadioButton_tree)).addContainerGap(27,
						Short.MAX_VALUE)));
		jPanel3Layout
				.setVerticalGroup(jPanel3Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel3Layout
										.createSequentialGroup()
										.addComponent(jRadioButton_combo)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jRadioButton_text)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(jRadioButton_tree)));

		jButton_delete.setText("delete");
		jButton_delete.setName("jButton_delete"); // NOI18N
		jButton_delete.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseEntered(java.awt.event.MouseEvent evt) {
				jButton_deleteMouseEntered(evt);
			}
		});
		jButton_delete.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_deleteActionPerformed(evt);
			}
		});

		jButton_close.setText("close");
		jButton_close.setName("jButton_close"); // NOI18N
		jButton_close.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_closeActionPerformed(evt);
			}
		});

		jButton_save.setText("save");
		jButton_save.setName("jButton_save"); // NOI18N
		jButton_save.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton_saveActionPerformed(evt);
			}
		});

		jLabel5.setText("Name");
		jLabel5.setName("jLabel5"); // NOI18N

		jTextField_name.setName("jTextField_name"); // NOI18N

		jLabel6.setText("Description");
		jLabel6.setName("jLabel6"); // NOI18N

		jScrollPane4.setName("jScrollPane4"); // NOI18N

		jTextArea1.setColumns(20);
		jTextArea1.setRows(3);
		jTextArea1.setName("jTextArea1"); // NOI18N
		jScrollPane4.setViewportView(jTextArea1);

		list_lists.setName("list_lists"); // NOI18N

		jLabel7.setText("List");
		jLabel7.setName("jLabel7"); // NOI18N

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout
				.setHorizontalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												list_var,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												156,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(10, 10, 10)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addGap(
																				34,
																				34,
																				34)
																		.addComponent(
																				jButton_delete)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jButton_insert))
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				jPanel2Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								list_domain,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								190,
																								Short.MAX_VALUE)
																						.addComponent(
																								jScrollPane2,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								190,
																								Short.MAX_VALUE))))
										.addContainerGap())
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel2Layout
										.createSequentialGroup()
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel5)
														.addComponent(jLabel6))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												24, Short.MAX_VALUE)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																jScrollPane4)
														.addComponent(
																jTextField_name,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																299,
																Short.MAX_VALUE)))
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												jPanel3,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												63, Short.MAX_VALUE)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(
																jButton_close)
														.addComponent(
																jButton_save))
										.addContainerGap())
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addGap(64, 64, 64)
										.addComponent(jLabel3)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												146, Short.MAX_VALUE)
										.addComponent(jLabel4).addGap(92, 92,
												92))
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jScrollPane1,
																javax.swing.GroupLayout.Alignment.TRAILING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																356,
																Short.MAX_VALUE)
														.addComponent(jLabel2))
										.addContainerGap())
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(jLabel7)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												list_lists,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												330, Short.MAX_VALUE)
										.addContainerGap()));
		jPanel2Layout
				.setVerticalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel2Layout
										.createSequentialGroup()
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel5)
														.addComponent(
																jTextField_name,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel6)
														.addComponent(
																jScrollPane4,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																64,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jLabel2)
										.addGap(1, 1, 1)
										.addComponent(
												jScrollPane1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												78,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																jPanel2Layout
																		.createSequentialGroup()
																		.addComponent(
																				list_lists,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				56,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addGroup(
																				jPanel2Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jLabel3)
																						.addComponent(
																								jLabel4)))
														.addComponent(jLabel7))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																jPanel2Layout
																		.createSequentialGroup()
																		.addComponent(
																				list_domain,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				84,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jScrollPane2,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				39,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				jPanel2Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jButton_insert)
																						.addComponent(
																								jButton_delete)))
														.addComponent(
																list_var,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																162,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addComponent(
																				jButton_save)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jButton_close)
																		.addGap(
																				46,
																				46,
																				46))
														.addGroup(
																jPanel2Layout
																		.createSequentialGroup()
																		.addComponent(
																				jPanel3,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addContainerGap()))));

		jSplitPane1.setRightComponent(jPanel2);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup().addGap(121, 121, 121)
						.addComponent(jLabel1).addContainerGap(125,
								Short.MAX_VALUE)).addComponent(jSplitPane1,
				javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addComponent(jLabel1)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(jSplitPane1)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void jRadioButton_comboActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jRadioButton_comboActionPerformed

	}// GEN-LAST:event_jRadioButton_comboActionPerformed

	private void jRadioButton_textActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jRadioButton_textActionPerformed

	}// GEN-LAST:event_jRadioButton_textActionPerformed

	private void jRadioButton_treeActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jRadioButton_treeActionPerformed

	}// GEN-LAST:event_jRadioButton_treeActionPerformed

	private void list_domainActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_list_domainActionPerformed

	}// GEN-LAST:event_list_domainActionPerformed

	private void list_varActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_list_varActionPerformed
		updateRangeList(this.list_var.getSelectedItem());
	}// GEN-LAST:event_list_varActionPerformed

	private void jTextArea_searchKeyPressed(java.awt.event.KeyEvent evt) {// GEN-FIRST:event_jTextArea_searchKeyPressed

	}// GEN-LAST:event_jTextArea_searchKeyPressed

	private void jButton_insertActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_insertActionPerformed
		addRange(this.jTextArea_search.getText());
	}// GEN-LAST:event_jButton_insertActionPerformed

	private void jButton_deleteActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_deleteActionPerformed

	}// GEN-LAST:event_jButton_deleteActionPerformed

	private void jTextArea_sparqlKeyPressed(java.awt.event.KeyEvent evt) {// GEN-FIRST:event_jTextArea_sparqlKeyPressed
		updateText(this.jTextArea_sparql.getText());
	}// GEN-LAST:event_jTextArea_sparqlKeyPressed

	private void list_varMouseEntered(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_list_varMouseEntered

	}// GEN-LAST:event_list_varMouseEntered

	private void list_domainMouseEntered(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_list_domainMouseEntered

	}// GEN-LAST:event_list_domainMouseEntered

	private void jTextArea_searchMouseEntered(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jTextArea_searchMouseEntered

	}// GEN-LAST:event_jTextArea_searchMouseEntered

	private void jButton_deleteMouseEntered(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jButton_deleteMouseEntered

	}// GEN-LAST:event_jButton_deleteMouseEntered

	private void jButton_insertMouseEntered(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jButton_insertMouseEntered

	}// GEN-LAST:event_jButton_insertMouseEntered

	private void jButton_saveActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_saveActionPerformed
		saveTemplate(null);
	}// GEN-LAST:event_jButton_saveActionPerformed

	private void jButton_closeActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton_closeActionPerformed
		dispose();
	}// GEN-LAST:event_jButton_closeActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton jButton_close;
	private javax.swing.JButton jButton_delete;
	private javax.swing.JButton jButton_insert;
	private javax.swing.JButton jButton_save;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JRadioButton jRadioButton_combo;
	private javax.swing.JRadioButton jRadioButton_text;
	private javax.swing.JRadioButton jRadioButton_tree;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	private javax.swing.JScrollPane jScrollPane3;
	private javax.swing.JScrollPane jScrollPane4;
	private javax.swing.JSplitPane jSplitPane1;
	private javax.swing.JTextArea jTextArea1;
	private javax.swing.JTextArea jTextArea_help;
	private javax.swing.JTextArea jTextArea_search;
	private javax.swing.JTextArea jTextArea_sparql;
	private javax.swing.JTextField jTextField_name;
	private java.awt.List list_domain;
	private java.awt.List list_lists;
	private java.awt.List list_var;
	// End of variables declaration//GEN-END:variables

	HashMap<String, String> variableList = null;
	HashMap<String, ArrayList<String>> domainList = new HashMap<String, ArrayList<String>>();
	ArrayList<SparqlList> sparqLists = null;

	private void updateText(String text) {

		CharStream cs = new ANTLRStringStream(text);
		twi_sparqlLexer lexer = new twi_sparqlLexer(cs);
		CommonTokenStream tokens = new CommonTokenStream();
		tokens.setTokenSource(lexer);
		twi_sparqlParser parser = new twi_sparqlParser(tokens);
		try {
			parser.query();
			this.variableList = parser.varMap;
			this.sparqLists = parser.list;
		} catch (RecognitionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		updateVariableList();
		updateLists();
	}

	private void updateLists() {
		this.list_lists.clear();
		for (int i = 0; i < this.sparqLists.size(); i++)
			this.list_lists.addItem(this.sparqLists.get(i).getText());
		validate();
	}

	private void updateVariableList() {
		this.list_var.clear();
		if (this.variableList == null)
			return;
		Iterator<String> iter = this.variableList.values().iterator();
		while (iter.hasNext()) {
			list_var.addItem(iter.next());
		}
		validate();
	}

	private void addRange(String domain) {
		String item = this.list_var.getSelectedItem();
		if (item == null)
			return;
		if (!this.domainList.containsKey(item))
			this.domainList.put(item, new ArrayList<String>());
		ArrayList<String> list = domainList.get(item);
		list.add(domain);
		updateRangeList(item);
	}

	private void updateRangeList(String var) {
		list_domain.clear();
		ArrayList<String> list = this.domainList.get(var);
		if (list == null)
			return;
		for (int i = 0; i < list.size(); i++)
			list_domain.addItem(list.get(i));
		validate();
	}

	public void saveTemplate(File file) {
		ArrayList<String> varNames = new ArrayList<String>();
		Iterator<String> iter = this.variableList.keySet().iterator();
		while (iter.hasNext()) {
			varNames.add(iter.next());
		}
		QueryTemplate qt = new QueryTemplate(varNames, this.domainList,
				this.sparqLists, this.jTextArea1.getText(),
				this.jTextField_name.getText(), this.jTextArea_sparql.getText());
		QueryTemplate.add(qt);
		TWI.getInstance().updateTemplates();
	}
}
